<?php
	class database{
		//Cau truy van
		var $_query = '';
		//ket noi
		var $_connection = '';
		//ket qua cua cau lenh truy van
		var $_cursor = NULL;
		
		//Ket noi CSDL 
		function database()
		{
			$this -> _connection = @mysql_connect('localhost','root','10');
			if (!$this -> _connection){
				die("Không thể kết nối cơ sở dữ liệu.");
				
			}
			$db = 'ql_ban_sua';
			if ($db != '' && !mysql_select_db($db, $this -> _connection)){
				die("Không thể mở CSDL $db: ".mysql_error());
			}
		}
		
		//Gan gia tri cho cau lenh truy van
        function setQuery($sql)
		{
            $this -> _query = $sql;
        }
		
		//Lay tat ca cac dong du lieu (mau tin)
		function loadAllRow()
		{
			//if (!($cur = $this-> query()))  //kiem tra cau truy van
            if (!($cur = $this-> safe_query()))  
				return NULL;
			$array = array();//khai bao mang de luu tru cac mau tin tu cau lenh truy van
			while ($row = mysql_fetch_assoc($cur))//lan luot duyet tung mau tin trong cau lenh truy van va gan vao mang
			{
				$array[] = $row;
			}
			mysql_free_result($cur);//Giai phong cau lenh truy van
			return $array;//Tra ve mang chua cac mau ti
					
		}
		
	
		
		//Dong ket noi CSDL
		function disconnect()
		{
			mysql_close($this->_connection);
		}
        
        function insert_row($table="", $atts="")
        {
            if(empty($table) || !is_array($atts))
            {
                return False;
            }
            else
            {
                while (list ($col, $val) = each ($atts))
                {
                    //if null go to the next array item
                    if ($val=="")
                    {
                        continue;
                    }
                    $col_str .= $col . ",";
                    if (is_int($val) || is_double ($val))
                    {
                        $val_str .= $val . ",";
                    }
                    else
                    {
                        $val_str .= "'$val',";
                    }
                }
                $this->_query = "insert into $table ($col_str) values($val_str)";
                //trim trailing comma from both strings
                $this->_query = str_replace(",)", ")", $this->_query);
            }
            $this->safe_query();
            //$this -> setQuery($query);
            //$this -> query();
            return mysql_affected_rows();
        }
        /*use insert_row
        $array = array('Ma_Khach_Hang'=>'kh008', 'Ten_Khach_Hang'=>'Khuat Thuy Dung', 'Phai' => 1,'Dia_Chi' => 'Ly thuong kiet','Dien_Thoai' => '0985883809','Email' => 'aaaaaaa@yahoo.com');
    
        if (!$database ->insert_row("khach_hang",$array))
        {
            echo "delete fail";
        }*/
       	//Thuc hien cau lenh truy van
		/*function query()
		{
			$this -> _cursor = mysql_query($this -> _query, $this -> _connection);
			return $this -> _cursor;
		}*/
        function safe_query ()
        {
            if (empty($this ->_query)) { return FALSE; }
            $this->_cursor = mysql_query($this->_query, $this->_connection)
          
            or die("ack! query failed: errorno=".mysql_errno()."<li>error=".mysql_error()."<li>query=".$this->_query);
            return $this->_cursor;
        }
         
        function delete_row($table="", $where="")
        {
            if (empty($table) || empty($where))
            {
                return FALSE;
            }
            $this->_query = "delete from $table where $where"; 
             $this->safe_query();// or die (mysql_error());
            //mysql_query($this->_query) or die (mysql_error());
            return mysql_affected_rows();
        }
        /*//use delete_row
        if (!$database ->delete_row("khach_hang","Ma_Khach_Hang='kh005'"))
        {
            echo "insert fail";
        }*/
        function update_row($table="", $atts="", $where="")
        {
            if(empty($table) || !is_array($atts))
            {
                return FALSE;
            }
            else
            {
                while(list ($col, $val) = each ($atts))
                {
                    if ($val=="")
                    {
                        continue;
                    }
                    if(is_int($val) || is_double($val))
                    {
                        $str .= "$col=$val,";
                    }
                    elseif($val=="NULL" || $val=="null")
                    {
                        $str .= "$col=NULL,";
                    }
                    else
                    {
                        $str .= "$col='$val',";
                    }
                }
            }
            $str = substr($str, 0, -1);
            $this->_query = "update $table set $str";
            if (!empty($where))
            {
                $this->_query .= " where $where"; 
            }
             $this->safe_query();// or die (mysql_error());
            //mysql_query($this->_query) or die (mysql_error());
            return mysql_affected_rows();
        }
        /*use update_row
        $array = array('Ten_Khach_Hang'=>'Vo Kieu Tuyet', 'Phai' => 1,'Dia_Chi' => 'Tan Hiep - Kien Giang','Dien_Thoai' => '0985883809','Email' => 'ngocbau175@yahoo.com');
        if (!$database ->update_row("khach_hang",$array,"Ma_Khach_Hang='kh007'"))
        {
            echo "update fail";
        }*/


        /*function safe_query ($query = "")
        {
            if (empty($query)) { return FALSE; }
            $result = mysql_query($query, $this->_connection)
          
            or die("ack! query failed: errorno=".mysql_errno()."<li>error=".mysql_error()."<li>query=".$query);
            return $result;
              //$this -> setQuery($query);
            //$result = $this -> query();
        }*/
        /*function insert_row($table="", $atts="")
        {
            if(empty($table) || !is_array($atts))
            {
                return False;
            }
            else
            {
                while (list ($col, $val) = each ($atts))
                {
                    //if null go to the next array item
                    if ($val=="")
                    {
                        continue;
                    }
                    $col_str .= $col . ",";
                    if (is_int($val) || is_double ($val))
                    {
                        $val_str .= $val . ",";
                    }
                    else
                    {
                        $val_str .= "'$val',";
                    }
                }
                $query = "insert into $table ($col_str) values($val_str)";
                //trim trailing comma from both strings
                $query = str_replace(",)", ")", $query);
            }
            $this->safe_query();
            //$this -> setQuery($query);
            //$this -> query();
            return mysql_affected_rows();
        }*/
        //This function takes an associative array and creates name=”value” pairs suitable for HTML tags.
        function get_attlist ($atts=””,$defaults=””)
        {
            $localatts = array();
            $attlist = "";
            if (is_array($defaults)) 
            { 
                $localatts = $defaults; 
            }
            if (is_array($atts)) 
            { 
                $localatts = array_merge($localatts,$atts); 
            }
            while (list($name,$value) = each($localatts))
            {
                if ($value == "") 
                { 
                    $attlist .= "$name "; 
                }
                else 
                { 
                    $attlist .= "$name=\"$value\" "; 
                }
            }
            return $attlist;
        }

        //This function creates an anchor tag.
        function anchor_tag($href="",$text="",$atts="")
        {
            $attlist = $this->get_attlist($atts,array("href"=>$href));
            $output = "<a $attlist>$text</a>";
            return $output;
        }
        //USE: anchor_tag("myurl.com/index.html", "this is a great link");
        //This creates an <img> tag. This function works just like the anchor_tag() function described above.
        function image_tag($src="",$atts="")
        {
            $attlist = get_attlist($atts,array("src"=>$src));
            $output = "img $attlist>";
            return $output;
        }
        function ExamExits($sql){
            $ok = false;
            $result = mysql_query($sql,$this->_connection);
            if (mysql_num_rows($result)>0)
                $ok = true;
            return $ok;
        }

	}
?>